package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.media.dtos.WmNewsDTO;
import com.heima.model.wmmedia.dtos.NewsAuthDTO;
import com.heima.model.wmmedia.dtos.WmNewsPageReqDTO;
import com.heima.model.wmmedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.heima.constants.message.WemediaConstants.WM_NEWS_AUTH_FAIL;
import static com.heima.constants.message.WemediaConstants.WM_NEWS_AUTH_PASS;

@RestController
@RequestMapping("/api/v1/news")
@Api(value = "自媒体文章管理API", tags = "自媒体文章管理API")
public class WmNewsController {
    @Autowired
    private WmNewsService wmNewsService;

    //分页带条件查询自媒体文章列表
    @PostMapping("/list")
    @ApiOperation("分页带条件查询自媒体文章列表")
    public ResponseResult listWmNews(@RequestBody WmNewsPageReqDTO dto) {
        return wmNewsService.listWmNews(dto);
    }

    //保存或修改文章
    @ApiOperation(value = "发表文章", notes = "发表文章，保存草稿，修改文章 共用的方法")
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDTO dto) {
        return wmNewsService.submitNews(dto);
    }

    //根据id查询文章
    @GetMapping("/one/{id}")
    @ApiOperation("根据id查询文章")
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    //根据id删除自媒体文章
    @GetMapping("/del_news/{id}")
    @ApiOperation("根据id删除自媒体文章")
    public ResponseResult deleteNews(@PathVariable("id") Integer id) {
        return wmNewsService.deleteNews(id);
    }

    //自媒体文章-上架、下架
    @PostMapping("/down_or_up")
    @ApiOperation("自媒体文章-上架、下架")
    public ResponseResult downOrUp(@RequestBody WmNewsDTO dto) {
        return wmNewsService.downOrUp(dto);
    }

    //分页查询文章和作者列表
    @PostMapping("/list_vo")
    @ApiOperation(("分页查询文章和作者列表"))
    public ResponseResult findList(@RequestBody NewsAuthDTO dto) {
        return wmNewsService.findList(dto);
    }

    //根据文章id查询文章和作者详情
    @GetMapping("/one_vo/{id}")
    @ApiOperation(("根据文章id查询文章和作者详情"))
    public ResponseResult findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }

    //文章审核成功
    @ApiOperation("人工审核通过 状态:4")
    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody NewsAuthDTO dto) {
        return wmNewsService.updateWmNewsStatus(dto, WM_NEWS_AUTH_PASS);
    }

    //文章审核失败
    @ApiOperation("人工审核失败 状态:2")
    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody NewsAuthDTO dto) {
        return wmNewsService.updateWmNewsStatus(dto, WM_NEWS_AUTH_FAIL);
    }

    //根据文章id修改文章
    @ApiOperation("根据id修改自媒体文章")
    @PutMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult();
    }
}